<?php
namespace app\admin\controller;
use think\Request;
use app\base\model\{
    User,
    Goods,
    Master,
    MasterVideo,
    AccessLog,
    Order
};
class Index extends Common{
    //用户模型
    protected $user;
    //商品模型
    protected $goods;
    //大师模型
    protected $master;
    //大师视频模型
    protected $master_video;
    //订单模型
    protected $order;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->user= new User();
        $this->goods=new Goods();
        $this->master= new Master();
        $this->master_video=new MasterVideo();
        $this->order = new Order();
    }
    /**
     *首页
     * return json
     */
  public function index(){
      if(empty($this->adminInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $platform_stats=[]; //平台统计
      //会员数量
      $platform_stats['user_count']=$this->user->count();
      //商品数量
      $platform_stats['goods_count']=$this->goods->where(['is_delete'=>0])->count();
      //入驻大师的数量
      $platform_stats['master_count']=$this->master->where(['is_delete'=>0])->count();
      //大师视频
      $platform_stats['master_video_count']=$this->master_video->where(['is_delete'=>0])->count();
      $goods_stats=[]; //商品统计
      $sale_total=0; //销售总数
      $store_notice_num=0; //库存紧张总数
      $hot_recom_num=0; //热门推荐商品总数
      $goods_list=$this->goods->where(['is_delete'=>0])->select();
      foreach($goods_list as $key=>$value){
          $sale_total+=$value['goods_sales_num'];
          if($value['goods_store_num']<20){
           $store_notice_num+=1;
          }
          if($value['is_hot']==1 || $value['is_recommend']==1){
             $hot_recom_num+=1;
          }
      }
      $goods_stats['sale_total']=$sale_total;
      $goods_stats['store_notice_num']=$store_notice_num;
      $goods_stats['hot_recom_num'] =$hot_recom_num;
      $accessModel=new AccessLog();
      $access_num= $accessModel->where(['date'=>date('Y-m-d',time())])->count();
      $goods_stats['access_num']=$access_num;
      $order_stats=[];  //订单统计
      $order_stats['shipping_order_count']=$this->order->where(['order_status'=>3,'is_delete'=>0])->count();
      $order_stats['finish_order_count']  =$this->order->where(['order_status'=>5,'is_delete'=>0])->count();
      $order_stats['notpay_order_count']  =$this->order->where(['order_status'=>1,'is_delete'=>0])->count();
      $order_stats['refund_order_count']  =$this->order->where(['order_status'=>7,'is_delete'=>0])->count();
      $msg['message']=checkMsg(GET_DATA_SUCCESS);
      $msg['platform_stats']=$platform_stats;
      $msg['goods_stats']   =$goods_stats;
      $msg['order_stats']   =$order_stats;
      return $msg;
  }
}